package com.augurit.wf.jbpmex.assignee;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.OmUserForm;

public class AssigneeUtils {
	
	public static final Long CONSTANT_NODE_ID = -1L;
	public static final String NODE_TYPE_ROOT = "ROOT";
	public static final String NODE_TYPE_ORG = "ORG";
	public static final String NODE_TYPE_GROUP = "GROUP";
	public static final String NODE_TYPE_POS = "POS";
	public static final String NODE_TYPE_USER = "USER";
	public static final String NODE_TYPE_ROLE = "ROLE";
	
	public static final String CLS_CHILD_ROOT = "icon-child-root";	
	
	public static final String ICON_ORG = "/resources/images/icons/16_16/organisation.png";
	public static final String ICON_GROUP = "/resources/images/icons/16_16/bricks.png";
	public static final String ICON_POS = "/resources/images/icons/16_16/group.png";
	public static final String ICON_USER_MALE = "/resources/images/icons/16_16/user.png";
	public static final String ICON_USER_FEMALE = "/resources/images/icons/16_16/user_female.png";
	public static final String ICON_ROLE = "/resources/images/icons/16_16/folder_user.png";
	
	
	
	public static boolean isRootNode(AssigneeTreeNode node){
		if(node != null && node.getType() != null && node.getType().equals(NODE_TYPE_ROOT))
			return true;
		else
			return false;
	}
	
	public static boolean isOrgNode(AssigneeTreeNode node){
		if(node != null && node.getType() != null && node.getType().equals(NODE_TYPE_ORG))
			return true;
		else
			return false;
	}
	
	public static boolean isGroupNode(AssigneeTreeNode node){
		if(node != null && node.getType() != null && node.getType().equals(NODE_TYPE_GROUP))
			return true;
		else
			return false;
	}
	
	public static boolean isPosNode(AssigneeTreeNode node){
		if(node != null && node.getType() != null && node.getType().equals(NODE_TYPE_POS))
			return true;
		else
			return false;
	}
	
	public static boolean isUserNode(AssigneeTreeNode node){
		if(node != null && node.getType() != null && node.getType().equals(NODE_TYPE_USER))
			return true;
		else
			return false;
	}
	
	public static boolean isRoleNode(AssigneeTreeNode node){
		if(node != null && node.getType() != null && node.getType().equals(NODE_TYPE_ROLE))
			return true;
		else
			return false;
	}
	
	
	public static List<AssigneeTreeNode> getRootNodeChildren(String assigneeRange){
		List<AssigneeTreeNode> result = new ArrayList();
		
		boolean isExistRoleNode = false;
		boolean isExistOrgNode = false;
		boolean isExistPosNode = false;
		boolean isExistGroupNode = false;
		boolean isExistUserNode = false;
		
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			String assignees[] =  assigneeRange.split(",");
			for(String assignee : assignees){
				if(!isExistRoleNode && assignee.indexOf(AssigneeParser.ASSIGNEE_ROLE_FLAG) >= 0){
					AssigneeTreeNode node =  new AssigneeTreeNode();
					node.setType(NODE_TYPE_ROLE);
					node.setRoleId(CONSTANT_NODE_ID);
					node.setText("角色");
					node.setIconCls(CLS_CHILD_ROOT);
					node.setExpanded(true);
					node.setDraggable(false);
					result.add(node);
					
					isExistRoleNode = true;
				}  
				else if(!isExistOrgNode && assignee.indexOf(AssigneeParser.ASSIGNEE_ORG_FLAG) >= 0){
					AssigneeTreeNode node =  new AssigneeTreeNode();
					node.setType(NODE_TYPE_ORG);
					node.setOrgId(CONSTANT_NODE_ID);
					node.setText("机构");
					node.setIconCls(CLS_CHILD_ROOT);
					node.setExpanded(true);
					node.setDraggable(false);
					result.add(node);
					
					isExistOrgNode = true;
				}
				else if(!isExistPosNode && assignee.indexOf(AssigneeParser.ASSIGNEE_POS_FLAG) >= 0){
					AssigneeTreeNode node =  new AssigneeTreeNode();
					node.setType(NODE_TYPE_POS);
					node.setPosId(CONSTANT_NODE_ID);
					node.setText("岗位");
					node.setIconCls(CLS_CHILD_ROOT);
					node.setExpanded(true);
					node.setDraggable(false);
					result.add(node);
					
					isExistPosNode = true;
				}
				else if(!isExistGroupNode && assignee.indexOf(AssigneeParser.ASSIGNEE_GROUP_FLAG) >= 0){
					AssigneeTreeNode node =  new AssigneeTreeNode();
					node.setType(NODE_TYPE_GROUP);
					node.setGroupId(CONSTANT_NODE_ID);
					node.setText("工作组");
					node.setIconCls(CLS_CHILD_ROOT);
					node.setExpanded(true);
					node.setDraggable(false);
					result.add(node);
					
					isExistGroupNode = true;
				}
				else if(!isExistUserNode){
					AssigneeTreeNode node =  new AssigneeTreeNode();
					node.setType(NODE_TYPE_USER);
					node.setUserId(CONSTANT_NODE_ID);
					node.setText("用户");
					node.setIconCls(CLS_CHILD_ROOT);
					node.setExpanded(true);
					node.setDraggable(false);
					result.add(node);
						
					isExistUserNode = true;
				}
			}
			
		}
		
		return result;
	}
	public static List<AssigneeTreeNode> getRootNodeChildren(String assigneeRange,boolean roleFlag, boolean orgFlag,boolean posFlag,boolean groupFlag,boolean userFlag){
		List<AssigneeTreeNode> result = new ArrayList();
		if(roleFlag){
			AssigneeTreeNode node =  new AssigneeTreeNode();
			node.setType(NODE_TYPE_ROLE);
			node.setRoleId(CONSTANT_NODE_ID);
			node.setText("角色");
			node.setIconCls(CLS_CHILD_ROOT);
			node.setExpanded(true);
			node.setDraggable(false);
			result.add(node);
		}
		if(orgFlag){
			AssigneeTreeNode node =  new AssigneeTreeNode();
			node.setType(NODE_TYPE_ORG);
			node.setOrgId(CONSTANT_NODE_ID);
			node.setText("机构");
			node.setIconCls(CLS_CHILD_ROOT);
			node.setExpanded(true);
			node.setDraggable(false);
			result.add(node);
		}
		if(posFlag){
			AssigneeTreeNode node =  new AssigneeTreeNode();
			node.setType(NODE_TYPE_POS);
			node.setPosId(CONSTANT_NODE_ID);
			node.setText("岗位");
			node.setIconCls(CLS_CHILD_ROOT);
			node.setExpanded(true);
			node.setDraggable(false);
			result.add(node);
		}
		if(groupFlag){
			AssigneeTreeNode node =  new AssigneeTreeNode();
			node.setType(NODE_TYPE_GROUP);
			node.setGroupId(CONSTANT_NODE_ID);
			node.setText("工作组");
			node.setIconCls(CLS_CHILD_ROOT);
			node.setExpanded(true);
			node.setDraggable(false);
			result.add(node);
		}
		
		//用户根节点
		if(AssigneeUtils.existUserExpression(assigneeRange) && userFlag){
				AssigneeTreeNode node =  new AssigneeTreeNode();
				node.setType(NODE_TYPE_USER);
				node.setUserId(CONSTANT_NODE_ID);
				node.setText("用户");
				node.setIconCls(CLS_CHILD_ROOT);
				node.setExpanded(true);
				node.setDraggable(false);
				result.add(node);
		}
		
		return result;
	}
	
	public static Set<String> getUserKeySet(List<OmUserForm> list){
		if(list != null && list.size()>0){
			Set<String> result = new HashSet();
			
			for(OmUserForm form : list)
				result.add(form.getLoginName());
			
			return result;
		}
		return null;
	}

	public static OmUserForm getLoginUserForm(){
		
		try {
			LoginUserForm loginUser = (LoginUserForm)Struts2Utils.getSessionAttribute(WebContextUtils.SES_LOGIN_USER);
			if(loginUser != null && loginUser.getUser() != null)
			return loginUser.getUser();
		
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			OmUserForm userForm = new OmUserForm();
			userForm.setLoginName("aosadmin");
			userForm.setUserId(236L);
			userForm.setUserCode("aosadmin");
			userForm.setPassword("123");
			userForm.setUserName("系统管理员");
			return userForm;
		}
		
			
	}
	
	public static String getAssigneeValue(String assignee){
		if(assignee != null && assignee.trim().length()>0){
			String[] result = assignee.split(AssigneeParser.ASSIGNEE_INNER_SEPARATOR);
			if(result.length == 2)
				return result[1];
		}
		return null;	
	}
	
	public static List<String> getRoleCodeList(String assigneeRange){
		return getNonUserCodeList(assigneeRange, AssigneeParser.ASSIGNEE_ROLE_FLAG);
	}
	
	public static List<String> getPosCodeList(String assigneeRange){
		return getNonUserCodeList(assigneeRange, AssigneeParser.ASSIGNEE_POS_FLAG);
	}
	
	public static List<String> getOrgCodeList(String assigneeRange){
		return getNonUserCodeList(assigneeRange, AssigneeParser.ASSIGNEE_ORG_FLAG);
	}
	
	public static List<String> getGroupCodeList(String assigneeRange){
		return getNonUserCodeList(assigneeRange, AssigneeParser.ASSIGNEE_GROUP_FLAG);
	}
	
	private static List<String> getNonUserCodeList(String assigneeRange, String nonUserSysFlag){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<String> result = new ArrayList();
			
			String[] assignees = assigneeRange.split(AssigneeParser.ASSIGNEE_SEPARATOR);
			for(String assignee : assignees){
				if(assignee.indexOf(nonUserSysFlag) == 0){					
					result.add(getAssigneeValue(assignee));
				}
			}
				
			return result;
		}
		return null;
	}
	
	public static List<String> getUserKeyList(String assigneeRange){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<String> result = new ArrayList();
			
			String[] assignees = assigneeRange.split(AssigneeParser.ASSIGNEE_SEPARATOR);
			for(String assignee : assignees){
				if(assignee.indexOf(AssigneeParser.ASSIGNEE_SYS_FLAG) == -1){					
					result.add(assignee);
				}
			}
				
			return result;
		}
		return null;
	}
	
	/**
	 * 判断表达式中是否有用户子表达式
	 * @param assigneeRange
	 * @return
	 */
	public static boolean existUserExpression(String assigneeRange){
		if(assigneeRange != null && assigneeRange.trim().length() > 0){
			String assignees[] =  assigneeRange.split(AssigneeParser.ASSIGNEE_SEPARATOR);
			for(String assignee : assignees){
				if(assignee.indexOf(AssigneeParser.ASSIGNEE_SYS_FLAG) < 0 )
					return true;
			}
		}
		return false;
	}
}
